#define _CRT_SECURE_NO_WARNINGS 1
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
class Solution {
public:
    int maxDistToClosest(vector<int>& seats) {
        int ans = 1, l = 0, n = seats.size();
        while (l < n && seats[l] == 0) {
            l++;
        }
        ans = max(ans, l);
        while (l < n) {
            int r = l + 1;
            while (r < n && seats[r] == 0) {
                r++;
            }
            if (r == n)
                ans = max(ans, r - l - 1);
            else
                ans = max(ans, (r - l) / 2);
            l = r;
        }
        return ans;
    }
};


class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>>ans;
        sort(nums.begin(), nums.end());
        int n = nums.size();
        for (int i = 0; i < n - 2; i++) {
            if (i > 0 && nums[i] == nums[i - 1])continue;
            int target = -nums[i];
            int left = i + 1, right = n - 1;
            while (left < right) {
                int x = nums[left] + nums[right];
                if (x == target) {
                    ans.push_back({ nums[i],nums[left],nums[right] });
                    while (left < right && nums[left] == nums[++left]);
                    while (left < right && nums[right] == nums[--right]);
                }
                else if (x < target) {
                    while (left < right && nums[left] == nums[++left]);
                }
                else {
                    while (left < right && nums[right] == nums[--right]);
                }
            }
        }
        return ans;
    }
};

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<vector<int>>ans;
        sort(nums.begin(), nums.end());
        int n = nums.size();
        for (int i = 0; i < n - 3; i++) {
            if (i > 0 && nums[i - 1] == nums[i])continue;
            for (int j = i + 1; j < n - 2; j++) {
                if (j > i + 1 && nums[j - 1] == nums[j])continue;
                int left = j + 1, right = n - 1;
                while (left < right) {
                    long long sum = (long long)nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum < target) while (left < right && nums[left] == nums[++left]);
                    else if (sum > target) while (left < right && nums[right] == nums[--right]);
                    else {
                        ans.push_back({ nums[i],nums[j],nums[left],nums[right] });
                        while (left < right && nums[left] == nums[++left]);
                        while (left < right && nums[right] == nums[--right]);
                    }
                }
            }
        }
        return ans;
    }
};